package cn.edu.nuc.film.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.nuc.film.common.PageControl;
import cn.edu.nuc.film.common.Pager;
import cn.edu.nuc.film.common.Parameter;
import cn.edu.nuc.film.common.UnitHelper;
import cn.edu.nuc.film.dao.FunctionMapper;
import cn.edu.nuc.film.dao.UserMapper;
import cn.edu.nuc.film.entity.Function;
import cn.edu.nuc.film.entity.User;
import cn.edu.nuc.film.service.interfaces.UserService;

@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private FunctionMapper functionMapper;
	
	/**
	 * 登录操作，成功后进入初始化界面（对应的角色获取对应的功能）
	 */
	public User login(User user) {
		User user1 = userMapper.selectByUsername(user.getUsername());
		/*System.out.println(user1);*/
		return user1;
	}
	
	public List<Function> initpage( User user){
		
		List<Function> list = functionMapper.initpage(user);
		return list;
	}
	
	
	/**
	 * 获取用户列表
	 */
	public Pager<User> userlist( PageControl pc, String name ) {
		
		/**
		 * 设置dao层的参数列表( @Param("start") int start, @Param("offset") int
		 * offset,@Param("name") String name )
		 */
		Parameter para = new Parameter();
		para.setStart((pc.getCurrentindex() - 1) * pc.getPagesize());
		para.setOffset(pc.getPagesize());
		para.setName(name);
		// 获取每页的具体数据
		List<User> list = userMapper.userlist(para);
		// 设置总记录数
		int pagecount = 0;
		if (name != null) {
			pagecount = list.size();
		} else {
			pagecount = userMapper.count();
		}
		Pager<User> pager = UnitHelper.execlist(pagecount, list, pc);
		return pager;
	}
	
	/**
	 * 删除用户，通过userid来进行删除
	 */
	public int deleteByPrimaryKey(Integer userid) {
		int count = userMapper.deleteByPrimaryKey(userid);
		return count;
	}
	
	/**
	 * 添加用户
	 */
	public int insert(User user) {
		int count = userMapper.insert(user);
		return count;
	}
	
	/**
	 * 查询用户信息
	 */
	public User selectByPrimaryKey(Integer userid) {
		User user = userMapper.selectByPrimaryKey(userid);
		return user;
	}
	/**
	 * 修改用户的信息
	 */
	public int updateByPrimaryKeySelective(User user) {
		int count = userMapper.updateByPrimaryKeySelective(user);
		return count;
	}

	

}
